package com.example.config;

import io.swagger.v3.oas.models.OpenAPI;
import io.swagger.v3.oas.models.info.Info;
import io.swagger.v3.oas.models.info.Contact;
import io.swagger.v3.oas.models.info.License;
import io.swagger.v3.oas.models.Components;
import io.swagger.v3.oas.models.security.SecurityScheme;
import io.swagger.v3.oas.models.security.SecurityRequirement;
import io.swagger.v3.oas.models.media.Content;
import io.swagger.v3.oas.models.media.MediaType;
import io.swagger.v3.oas.models.parameters.HeaderParameter;
import io.swagger.v3.oas.models.examples.Example;
import org.springdoc.core.models.GroupedOpenApi;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.HttpHeaders;

import java.util.HashMap;
import java.util.Map;

/**
 * Knife4j 配置类
 */
@Configuration
public class SwaggerConfig {

    @Bean
    public OpenAPI customOpenAPI() {
        // 定义JWT安全方案
        SecurityScheme securityScheme = new SecurityScheme()
                .type(SecurityScheme.Type.HTTP)
                .scheme("bearer")
                .bearerFormat("JWT")
                .in(SecurityScheme.In.HEADER)
                .name("token");
        
        // 全局安全需求
        SecurityRequirement securityRequirement = new SecurityRequirement().addList("bearerAuth");
        
        // 添加示例
        Map<String, Example> examples = new HashMap<>();
        Example successExample = new Example();
        successExample.setValue("{\"code\":200,\"msg\":\"操作成功\",\"data\":null}");
        examples.put("success", successExample);
        
        Example errorExample = new Example();
        errorExample.setValue("{\"code\":500,\"msg\":\"操作失败\",\"data\":null}");
        examples.put("error", errorExample);
        
        return new OpenAPI()
                .info(new Info()
                        .title("员工管理系统 API")
                        .description("员工管理系统后端接口文档，提供系统所有API的详细说明")
                        .version("1.0")
                        .contact(new Contact()
                                .name("管理员")
                                .email("admin@example.com")
                                .url("https://www.example.com"))
                        .license(new License().name("Apache 2.0").url("https://www.apache.org/licenses/LICENSE-2.0")))
                .components(new Components()
                        .addSecuritySchemes("bearerAuth", securityScheme)
                        .addParameters("Accept-Language", new HeaderParameter()
                                .name(HttpHeaders.ACCEPT_LANGUAGE)
                                .description("语言设置")
                                .required(false)))
                .addSecurityItem(securityRequirement);
    }
    
    @Bean
    public GroupedOpenApi publicApi() {
        return GroupedOpenApi.builder()
                .group("public-api")
                .pathsToMatch("/**")
                .build();
    }
    
    @Bean
    public GroupedOpenApi employeeApi() {
        return GroupedOpenApi.builder()
                .group("员工管理")
                .pathsToMatch("/emps/**")
                .build();
    }
    
    @Bean
    public GroupedOpenApi departmentApi() {
        return GroupedOpenApi.builder()
                .group("部门管理")
                .pathsToMatch("/depts/**")
                .build();
    }
    
    @Bean
    public GroupedOpenApi fileApi() {
        return GroupedOpenApi.builder()
                .group("文件管理")
                .pathsToMatch("/upload")
                .build();
    }
    
    @Bean
    public GroupedOpenApi authApi() {
        return GroupedOpenApi.builder()
                .group("认证管理")
                .pathsToMatch("/login")
                .build();
    }
} 